package cn.wenhaha.wxBox.spellbook

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.widget.LinearLayout
import android.widget.TextView
import cn.wenhaha.wxBox.util.DisplayUtils
import com.gh0u1l5.wechatmagician.spellbook.interfaces.ISearchBarConsole

object  Alert:ISearchBarConsole {

    private val mainHandler by lazy { Handler(Looper.getMainLooper()) }

    override fun onHandleCommand(context: Context, command: String): Boolean {
        if (command.startsWith("alert ")) {
            val prompt = TextView(context).apply {
                setTextColor(Color.BLACK)
                text = command.drop("alert ".length)
                textSize = 16.0F
                layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT).apply {
                    topMargin = DisplayUtils.dp2px(context,10f)
                    bottomMargin = DisplayUtils.dp2px(context,10f)
                    leftMargin = DisplayUtils.dp2px(context,25f)
                    rightMargin = DisplayUtils.dp2px(context,25f)
                }
            }
            val content = LinearLayout(context).apply {
                addView(prompt)
                layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT)
            }
            mainHandler.post {
                AlertDialog.Builder(context)
                    .setTitle("Wechat Magician")
                    .setView(content)
                    .show()
            }
            return true
        }
        return super.onHandleCommand(context, command)
    }
}

